package com.china08.yxyapi.api.manage.edu.platform.continuedu.lesson;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduLesson;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduLessonRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyContinueduLessonUpdStatusController extends YxyApiBaseController {

    @Autowired
    YxyContinueduLessonRepository conLessonRepository;

    @ApiOperation(value = "")
    @RequestMapping(value = "/api_yw/manage/edu/platform/continuedu/lesson/upd/status", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String lessonId = reqModel.lessonId;
        Integer status = reqModel.status;
        String rejectReason = reqModel.rejectReason;
        List<String> rejectReasonList = reqModel.rejectReasonList;

        Date date = new Date();
        YxyContinueduLesson lesson = conLessonRepository.findById(lessonId);
        if (lesson != null) {
            // 选择了下架原因
            if (rejectReasonList.size() > 0) {
                // 选择了自定义原因
                if (StringUtils.isNotBlank(rejectReason)) {
                    rejectReason = StringUtils.join(rejectReasonList.toArray(), ",") + "," + rejectReason;
                } else {
                    rejectReason = StringUtils.join(rejectReasonList.toArray(), ",");
                }
            }
            lesson.setIsVisible(status);
            lesson.setRejectReason(rejectReason);
            lesson.setLastUpDate(date);
            if (status == 1) {
                lesson.setUpDate(date);
            }
            if (status == 2) {
                lesson.setDownDate(date);
            }
            conLessonRepository.save(lesson);
        }
    }

    public static class ReqModel extends YxyApiReqModel {
        public String lessonId;
        public Integer status;
        public String rejectReason;
        public List<String> rejectReasonList;

        @Override
        public void autoCheck() {
        }
    }

}
